
//获取cookie、
export function getCookie(name) {
 var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
 if (arr = document.cookie.match(reg))
  return (arr[2]);
 else
  return null;
}
 
//设置cookie,增加到vue实例方便全局调用
export function setCookie (c_name, value, expiredays) {
 var exdate = new Date();
 exdate.setDate(exdate.getDate() + expiredays);
 document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
};
 
//删除cookie
export function delCookie (name) {
 var exp = new Date();
 exp.setTime(exp.getTime() - 1);
 var cval = getCookie(name);
 if (cval != null)
  document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
};


export function getQueryString(name) {
	var reg = new RegExp("(^|&\?)" + name + "=([^&]*)(&|$)");
	var r = window.location.href.match(reg);
	if(r != null) {
		return unescape(r[2]);
	}
	return null;
}

//传bb
//https://www.abc.com/aaa?cc=1&bb=2&aa=1
//=> 
//https://www.abc.com/aaa?cc=1&aa=1
export function cutQueryString(name, where ,oldUrl, newpage) {
	var url = oldUrl == undefined ? window.location.href : oldUrl;
	var reg ;
	if(where == 'before'){
		reg = new RegExp("(^|&\?)" + name + "=([^#]*)(#|$)");
	}else if(where == 'page'){
		reg = new RegExp("#/" + name + "?");
	}else{
		reg = new RegExp("(^|&\?)" + name + "=([^&]*)(&|$)");
	}
	console.log(reg);
	var r = url.match(reg);
	console.log(r);
	if(r != null) {
		console.log(r);
		var urls = url.split(r[0]);
		console.log(urls);
		var result = '';
		if(r[3] != ''){
			if(where == 'before'){
				result = urls[0] + '#' + urls[1];
			}else if(where == 'page'){
				result = urls[0] + '#/' + newpage + urls[1];
			}else{
				result = urls[0] + '&' + urls[1];
			}
		}else{
			result = urls[0] + urls[1];
		}
		return result;
	}
	return url;
}




export function str2date(strDate){

	if(typeof strDate === 'string')
	{
		var objDate = new Date(Date.parse(strDate.replace(/-/g, "/")));

		return objDate;
	}
	else
	{
		return '错误格式';
	}
	
}


export function date2str(objDate){

	if (typeof objDate === 'object') {
		var y = objDate.getFullYear();
		var m = objDate.getMonth() + 1;
		var d = objDate.getDate();
		m = m < 10? '0' + m: m;
		d = d < 10? '0' + d: d;
		return y + '-' + m + '-' + d;
	}
	else {
		return '错误格式';
	}
	
}


export function cutImage (obj) {
	var width = obj.w;
	var height = obj.h;

	var x = '';
	var y = '';
	var w = '';
	var h = '';

	if(width > height){
		x = (width - height) * 0.5;
		y = 0;
		w = height;
		h = height;
	}else{
		x = 0;
		y = (height - width) * 0.5;
		w = width;
		h = width;
	}

	return obj.url + '?x-oss-process=image/crop,' + 'x_' + x + ',y_' + y + ',w_' + w + ',h_' + h;
};



export function imgUtil(src) {
  return new Promise((resolve, reject) => {
      let img = new Image()
      img.src = src
      img.onload = function () {
        resolve({
          width: img.width,
          height: img.height
        })
      }
  })
}
//返回两个日期之间的所有日期
export function dataScope (value1, value2) {  
    
    var date1 = new Date(value1);
    var date2 = new Date(value2);
    // console.log(date1);
    // console.log(date2);

    if (date1 > date2) {  
        var tempDate = date1;  
        date1 = date2;  
        date2 = tempDate;  
    }  
    date2.setDate(date2.getDate() + 1);  
    var dateArr = [];  

    // console.log('月份比较');
    // console.log(date1.getFullYear() + '-' + parseInt(date1.getMonth())+1 + '-' + date1.getDate());
    // console.log(date2.getFullYear() + '-' + parseInt(date2.getMonth())+1 + '-' + date2.getDate());

    while (!(date1.getFullYear() == date2.getFullYear()  
            && date1.getMonth() == date2.getMonth() 
            && date1.getDate() == date2.getDate())) {  

        var dayStr =date1.getDate().toString();
        if(dayStr.length ==1){  
            dayStr="0"+dayStr;  
        }  
        var month = (date1.getMonth() + 1).toString();
        if(month.length == 1) month = '0' + month;
        dateArr.push(date1.getFullYear() + "-" + 
        			month + "-"  
                	+ dayStr);  
        date1.setDate(date1.getDate() + 1);  
    }  
    return dateArr;  
}		
